<template>
  <div class="user-edit-container">
    <!-- Header 区域 -->
    <van-nav-bar
      title="编辑资料"
      left-arrow
      @click-left="$router.back()"
      fixed
    />

    <!-- 用户资料 -->
    <van-cell-group class="action-card">
      <van-cell title="头像" is-link center>
        <template #default>
          <van-image
            round
            class="avatar"
            :src="userPorfileObj.photo"
            @click="choosePhoto()"
          />
          <!-- file 选择框 -->
          <input
            type="file"
            ref="iptFile"
            v-show="false"
            accept="image/*"
            @change="onFileChange"
          />
        </template>
      </van-cell>
      <van-cell
        title="名称"
        :value="userPorfileObj.name"
        is-link
        @click="userFn"
      />
      <!-- <van-cell title="性别" is-link  /> -->
      <van-cell title="生日" :value="userPorfileObj.birthday" is-link @click="birthFn" />
    </van-cell-group>

    <!-- 用户名修改弹窗 -->
    <van-dialog
      v-model="show"
      title="修改用户名称"
      show-cancel-button
      :beforeClose="beforeCloseFn">
      <input type="text" placeholder="请输入用户名" v-fofo v-model="userName" />
    </van-dialog>
    <!-- 生日弹窗 -->
    <van-popup v-model="birshow" position="bottom" :style="{ height: '50%' }">
    <van-datetime-picker
      v-model="currentDate"
      type="date"
      title="选择年月日"
      :min-date="minDate"
      :max-date="maxDate"
      @cancel="birshow=false"
      @confirm="birthdayFn"
    /></van-popup>
  </div>
</template>

<script>
// updatePhotoAPI
import { userProfileAPI, updatePhotoAPI, updateProfileAPI } from '@/api'
import { MyNotify } from '@/utils/Notify'
// import moment from 'moment'
import { getCustomTime } from '@/utils/date'
import { mapMutations } from 'vuex'
export default {
  name: 'UserEdit',
  data () {
    return {
      userPorfileObj: {}, // 用户信息对象
      show: false, // 修改用户弹窗
      userName: '', // 用户名
      birshow: false, // 生日选择
      minDate: new Date(1900, 0, 1),
      maxDate: new Date(),
      currentDate: new Date()
    }
  },
  methods: {
    ...mapMutations(['updatePhoto', 'updateName']),
    // 点击图片后执行file的方法
    choosePhoto () {
      this.$refs.iptFile.click()
    },
    // input file 框的执行方法
    async onFileChange (e) {
      console.log(1)
      const fd = new FormData()
      fd.append('photo', e.target.files[0])
      const res = await updatePhotoAPI(fd)
      // console.log(res)
      this.userPorfileObj.photo = res.data.data.photo
      this.updatePhoto(res.data.data.photo)
    },
    // 用户名称的方法
    userFn () {
      this.show = true
      this.userName = this.userPorfileObj.name
    },
    // beforeCloseFn 用户关闭前弹窗的验证
    async beforeCloseFn (action, done) {
      // action 的值 confirm 确定 cancel 取消
      // console.log(action)
      if (action === 'confirm') {
        if (/^[A-Za-z0-9_\u4e00-\u9fa5]{1,7}$/.test(this.userName)) {
          await updateProfileAPI({ userName: this.userName })
          this.userPorfileObj.name = this.userName
          this.updateName(this.userName)
          done()
        } else {
          MyNotify({
            type: 'warning',
            message: '用户名格式是中文数字或字母的1-7位数'
          })
          done(false)
        }
      } else if (action === 'cancel') {
        done()
      }
    },
    // 点击生日按钮时
    birthFn () {
      this.birshow = true
      this.currentDate = new Date(this.userPorfileObj.birthday)
      // 组件日期要对象
      // 后台要日期字符串
    },
    // 点击生日弹窗 确定时
    async birthdayFn () {
      // const bt = moment(this.currentDate).format('YYYY-MM-DD')
      const bt = getCustomTime(this.currentDate)
      await updateProfileAPI({
        birthday: bt
      })
      this.userPorfileObj.birthday = bt
      this.birshow = false
    }
  },
  async created () {
    // 获取用户信息
    const res = await userProfileAPI()
    console.log(res)
    this.userPorfileObj = res.data.data
    // this.userName = res.data.data.name
  }
}
</script>

<style lang="less" scoped>
.user-edit-container {
  padding-top: 46px;
  .avatar {
    width: 50px;
    height: 50px;
  }
}
/deep/ .van-dialog__content {
  text-align: center;
  input {
    margin: 15px 0 15px 0;
    border: none;
    text-align: center;
    font-size: 14px;
  }
}
</style>
